<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\exception\ValidateException;

use app\common\model\{Project, Task, User};
use app\common\validate\{ImgValidate, ProjectValidate, TaskValidate};

class HomeController extends BaseController
{
    /** 上传图片 */
    public function img_up()
    {
        $data = input('');

        try{
            validate(ImgValidate::class)->scene('img')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $files = request()->file();

        if(!$files){
            $this->error_back('上传文件为空');
        }

        $imgs = [];
        foreach($files as $file){
            if(explode('/', $file->getMime())[0] !== 'image'){
                continue;
            }
            
            $imgs[] = fileUp($file, $data['path']);
        }

        $this->success_back('上传成功', $imgs);
    }

    /** 身份证识别 */
    public function getCardId()
    {
        $info = [
            'card_id'  => '130125199901010000',
            'card_img' => '/1.jpg',
        ];

        $this->success_back('ok', $info);
    }

    /** 项目列表 */
    public function getProjectList()
    {
        $data = input('');

        try{
            validate(ProjectValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $projectModel = new Project();

        $where[] = ['user_id' , '=', $data['user_id']];

        $list = $projectModel->getList($where, 0, 0, 0, 'id, name');

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        $this->success_back('ok', $list['data']);
    }

    /** 绑定项目 */
    public function bindProject()
    {
        $data = input('');

        try{
            validate(ProjectValidate::class)->scene('bind')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $userModel = new User();

        $where[] = ['user_id' , '=', $data['user_id']];

        $flag = $userModel->edit(['id' => $data['user_id'], 'bind_project_id' => $data['project_id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('绑定成功');
    }

    /** 发布任务 */
    public function addTask()
    {
        $data = input('');

        try{
            validate(TaskValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $taskModel = new Task();

        $data['finish_time'] = strtotime($data['finish_time']);

        $flag = $taskModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('发布成功');
    }

    /** 编辑任务 */
    public function updateTask()
    {
        $data = input('');

        try{
            validate(TaskValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $taskModel = new Task();

        $data['id'] = $data['task_id'];
        $data['finish_time'] = strtotime($data['finish_time']);
        unset($data['user_id']);

        $flag = $taskModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('编辑成功');
    }

    /** 任务列表 */
    public function getTaskList()
    {
        $data = input('');

        try{
            validate(TaskValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $taskModel = new Task();
        $userModel = new User();

        $userInfo = $userModel->getFind(['id' => $data['user_id']]);

        if($userInfo['code'] !== 'ok' || $userInfo['data']['bind_project_id'] == 0){
            $this->error_back('暂无数据');
        }

        $where[] = ['user_id' , '=', $data['user_id']];
        $where[] = ['status' , '=', $data['status']];
        $where[] = ['project_id' , '=', $userInfo['data']['bind_project_id']];

        $list = $taskModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back($list['msg']);
        }

        $userModel = new User();
        $projectModel = new Project();

        foreach($list['data'] as $k => &$v){
            if($v['finish_time'] == -1 && $v['status'] == 1){
                $taskModel->edit(['id' => $v['id'], 'status' => 4]);
                unset($list['data'][$k]);
                continue;
            }

            $v['user_name'] = $userModel->getVal(['id' => $v['user_id']], 'nickname')['data'];
            $v['project_name'] = $projectModel->getVal(['id' => $v['project_id']], 'name')['data'];
            unset($v['project_id']);
        }

        if($list['data']->isEmpty()){
            $this->error_back('暂无数据');
        }

        $this->success_back('ok', $list['data']);
    }

    /** 任务完成 */
    public function taskFinish()
    {
        $data = input('');

        try{
            validate(TaskValidate::class)->scene('finish')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $taskModel = new Task();

        $info = $taskModel->getFind(['id' => $data['task_id']]);

        if($info['code'] != 'ok'){
            $this->error_back($info['msg']);
        }

        if($info['data']['to_user_id'] != $data['to_user_id']){
            $this->error_back('任务不存在');
        }

        if($info['data']['status'] != 1){
            $this->error_back('任务状态错误');
        }

        $flag = $taskModel->edit(['id' => $data['task_id'], 'status' => 2]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('完成成功');
    }

    /** 评价任务 */
    public function taskEvaluate()
    {
        $data = input('');

        try{
            validate(TaskValidate::class)->scene('eval')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $taskModel = new Task();

        $info = $taskModel->getFind(['id' => $data['task_id']]);

        if($info['code'] != 'ok'){
            $this->error_back($info['msg']);
        }

        if($info['data']['to_user_id'] != $data['to_user_id']){
            $this->error_back('任务不存在');
        }

        if($info['data']['status'] != 2){
            $this->error_back('任务状态错误');
        }

        $data['id'] = $data['task_id'];
        $data['status'] = 3;
        unset($data['user_id']);

        $flag = $taskModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('评价成功');
    }
}
